﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Input;
using System.Windows.Media;
using wBooks.Entity;

namespace wBooks
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        public SettingsPage()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            //Set current value to settings page
            this.FontPicker.SelectedItem = Sessions.Font.Value;
            this.PageColorButton.Background = new SolidColorBrush(Sessions.PageColor.Value);
            this.TextColorButton.Background = new SolidColorBrush(Sessions.TextColor.Value);
            this.SampleBackground.Fill = this.PageColorButton.Background;
            this.SampleTextBlock.Foreground = this.TextColorButton.Background;
            this.SampleTextBlock.FontFamily = new FontFamily(Sessions.Font.Value);
            this.TextSizeSlider.Value = Sessions.TextSize.Value;
        }

        void PageColorButton_Click(object sender, EventArgs e)
        {
            // Get a string representation of the colors we need to pass to the color
            // picker, without the leading #
            string currentColorString = Sessions.PageColor.Value.ToString().Substring(1);
            string defaultColorString =
              Sessions.PageColor.DefaultValue.ToString().Substring(1);

            // The color picker works with the same isolated storage value that the
            // Setting works with, but we have to clear its cached value to pick up
            // the value chosen in the color picker
            Sessions.PageColor.ForceRefresh();

            // Navigate to the color picker
            this.NavigationService.Navigate(new Uri(
              "/ColorPickerPage.xaml?"
              + "showOpacity=false"
              + "&currentColor=" + currentColorString
              + "&defaultColor=" + defaultColorString
              + "&settingName=PageColor", UriKind.Relative));
        }

        void TextColorButton_Click(object sender, EventArgs e)
        {
            // Get a string representation of the colors, without the leading #
            string currentColorString = Sessions.TextColor.Value.ToString().Substring(1);
            string defaultColorString =
              Sessions.TextColor.DefaultValue.ToString().Substring(1);

            // The color picker works with the same isolated storage value that the
            // Setting works with, but we have to clear its cached value to pick up
            // the value chosen in the color picker
            Sessions.TextColor.ForceRefresh();

            // Navigate to the color picker
            this.NavigationService.Navigate(new Uri(
              "/ColorPickerPage.xaml?"
              + "showOpacity=false"
              + "&currentColor=" + currentColorString
              + "&defaultColor=" + defaultColorString
              + "&settingName=TextColor", UriKind.Relative));
        }

        void TextSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            // Gets called during InitializeComponent
            if (this.TextSizeSlider != null)
            {
                int textSize = (int)Math.Round(this.TextSizeSlider.Value);
                Sessions.TextSize.Value = textSize;
                this.SampleTextBlock.FontSize = textSize;
            }
        }

        void ResetButton_Click(object sender, RoutedEventArgs e)
        {
            //int textSize = Sessions.TextSize.DefaultValue;
            //this.TextSizeSlider.Value = textSize;
            //Sessions.TextSize.Value = textSize;
            //this.SampleTextBlock.FontSize = textSize;
        }

        void FontPicker_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.FontPicker != null)
            {
                Sessions.Font.Value = (string)this.FontPicker.SelectedItem;
                this.SampleTextBlock.FontFamily = new FontFamily(Sessions.Font.Value);
            }
        }
    }
}